import { app } from '../../../config/index';

Page({
  data: {
    personInfo: app.globalData.userInfo
  },
  onShow(){
    this.setData({
      personInfo: app.globalData.userInfo
    })
  },
  onChooseAvatar(e) {
    let avatarUrl = 'data:image/jpeg;base64,' +     wx.getFileSystemManager().readFileSync(e.detail.avatarUrl,'base64')
    wx.cloud.database().collection('book-user').where({
      _id: app.globalData.userInfo._id,
    }).update({
      data:{
        avatarUrl: avatarUrl,
        _updateTime: Date.now(),
      },
      success:(res)=>{
        this.setData({
          'personInfo.avatarUrl': avatarUrl
        })
        app.globalData.userInfo.avatarUrl = avatarUrl
      }
    })
  },
  onClickCell({ currentTarget }) {
    const { dataset } = currentTarget;
    const { nickName } = app.globalData.userInfo;

    switch (dataset.type) {
      case 'name':
        wx.navigateTo({
          url: `/pages/usercenter/name-edit/index?name=${nickName}`,
        });
        break;
      default: {
        break;
      }
    }
  }
});
